package com.erp.myAdapter;

import androidx.appcompat.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.erp.bean.ProdyctionClientBean;
import com.erp.R;
import com.erp.util.JsonUtil;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/23/023.
 * 作者：刘洋
 */
public class BaomingxiAdapter extends BaseAdapter {
    private List<ProdyctionClientBean.baomingxi> mlist;
    private Context mcontext;
    private LayoutInflater minflater;
    private int isvisi=-1;

    public BaomingxiAdapter(List<ProdyctionClientBean.baomingxi> list, Context context) {
        super();
        this.mlist = list;
        this.mcontext = context;
        this.minflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mlist.size();
    }

    @Override
    public Object getItem(int i) {
        return mlist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    public void itemposition(int poistion){
        if(isvisi==poistion){
            isvisi=-1;
        }else {
            isvisi = poistion;
        }
        notifyDataSetChanged();
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder vh;
        if (view == null) {
            vh = new ViewHolder();
            view = minflater.inflate(R.layout.lv_baomingxi_item, null);
            vh.tv_guihao = (TextView) view.findViewById(R.id.tv_guihao);
            vh.tv_zhuaungtai = (TextView) view.findViewById(R.id.tv_zhuaungtai);
            vh.tv_baohao = (TextView) view.findViewById(R.id.tv_baohao);
            vh.tv_leixing = (TextView) view.findViewById(R.id.tv_leixing);
            vh.tv_ruchangyuan = (TextView) view.findViewById(R.id.tv_ruchangyuan);
            vh.tv_rucangshijian = (TextView) view.findViewById(R.id.tv_rucangshijian);
            vh.tv_fahuoyuan = (TextView) view.findViewById(R.id.tv_fahuoyuan);
            vh.tv_fahuoshijian = (TextView) view.findViewById(R.id.tv_fahuoshijian);
            vh.tv_checi = (TextView) view.findViewById(R.id.tv_checi);
            vh.tv_tiaoxingma = (TextView) view.findViewById(R.id.tv_tiaoxingma);
            vh.tv_beizhu = (TextView) view.findViewById(R.id.tv_beizhu);
            vh.ll_xiangxi= (LinearLayout) view.findViewById(R.id.ll_xiangxi);
            vh.ll_baojiandan= (LinearLayout) view.findViewById(R.id.ll_baojiandan);
            vh.tv_cangwei= (TextView) view.findViewById(R.id.tv_cangwei);
            vh.tv_baowuliao= (TextView) view.findViewById(R.id.tv_baowuliao);
            vh.tv_pallet_num= (TextView) view.findViewById(R.id.tv_pallet_num);
            vh.tv_position_buffer= (TextView) view.findViewById(R.id.tv_position_buffer);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        ProdyctionClientBean.baomingxi baominghao = new ProdyctionClientBean.baomingxi();
        baominghao = mlist.get(i);
        settv(baominghao.guihao, vh.tv_guihao);
        settv(baominghao.zhuangtai, vh.tv_zhuaungtai);
        settv(baominghao.leixing, vh.tv_leixing);
        settv(baominghao.baohao, vh.tv_baohao);
        settv(baominghao.rucangyuan, vh.tv_ruchangyuan);
        settv(baominghao.rucangshijian, vh.tv_rucangshijian);
        settv(baominghao.fahuoyuan, vh.tv_fahuoyuan);
        settv(baominghao.fahuoshijian, vh.tv_fahuoshijian);
        settv(baominghao.checi, vh.tv_checi);
        settv(baominghao.tiaoxingma, vh.tv_tiaoxingma);
        settv(baominghao.beizhu, vh.tv_beizhu);
        settv(baominghao.position, vh.tv_cangwei);
        settv(baominghao.pallet_num, vh.tv_pallet_num);
        settv(baominghao.position_buffer, vh.tv_position_buffer);
        if(isvisi==i){
            vh.ll_xiangxi.setVisibility(View.VISIBLE);
            vh.ll_xiangxi.setBackgroundResource(R.color.gainsboro);
        }else{
            vh.ll_xiangxi.setVisibility(View.GONE);
            vh.ll_xiangxi.setBackgroundResource(R.color.white);
        }
        final ProdyctionClientBean.baomingxi finalBaominghao = baominghao;
        vh.tv_baowuliao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wuliaodialog(finalBaominghao.material);
            }
        });
        return view;
    }

    private void wuliaodialog(String wuliao) {
        AlertDialog wuliaoadialog = new AlertDialog.Builder(mcontext).create();
        wuliaoadialog.show();
        wuliaoadialog.getWindow().setContentView(R.layout.dialog_wuliao);
        ListView lvwuliao= (ListView) wuliaoadialog.findViewById(R.id.lv_baowuliao);
        ArrayList<ProdyctionClientBean.wuliaomingxi> baowuliaolist=new ArrayList<>();
        addbaowuliaolist(wuliao,baowuliaolist);
        final WuliaoAdapter adapter = new WuliaoAdapter(mcontext, baowuliaolist);
        Log.e("TAAAG","ada="+adapter+"   lv="+lvwuliao);
        lvwuliao.setAdapter(adapter);
        lvwuliao.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.isitem(position);
            }
        });
    }

    private void addbaowuliaolist(String wuliaos,ArrayList<ProdyctionClientBean.wuliaomingxi> baowuliaolist) {
        if(wuliaos==null){
            return;
        }
        try{
            JSONArray js=new JSONArray(wuliaos);
            JsonUtil.addwuliaos(baowuliaolist,js);
        }catch (JSONException e){
            e.printStackTrace();
        }
    }

    private void settv(String s, TextView tv) {
        if(s==null){
            return;
        }
        if (s.equals("null")) {
            tv.setText("");
        } else {
            tv.setText(s);
        }
    }

    private class ViewHolder {
        TextView tv_guihao, tv_zhuaungtai, tv_baohao, tv_leixing, tv_ruchangyuan, tv_rucangshijian, tv_fahuoyuan,
                tv_fahuoshijian, tv_checi, tv_tiaoxingma, tv_beizhu,tv_cangwei,tv_baowuliao,tv_pallet_num,tv_position_buffer;
        LinearLayout ll_xiangxi,ll_baojiandan;
    }
    public void  notifyall(List<ProdyctionClientBean.baomingxi> list){
        mlist=list;
        notifyDataSetChanged();
    }
}
